/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectProcessDlg.java
 *
 * Created on Dec 24, 2009, 5:07:24 PM
 */
package dnl.util.ui;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author daniel
 */
public class SelectionDlg extends Dialog {

	/** Creates new form SelectProcessDlg */
	public SelectionDlg(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public void setData(List<?> data) {
		List<DataWrapper> wrappedData = new ArrayList<DataWrapper>();
		for (int i = 0; i < data.size(); i++) {
			Object object = data.get(i);
			wrappedData.add(new DataWrapper(object, i));
		}
		itemsList.setListData(wrappedData.toArray());
	}

	public int getSelectedIndex() {
		DataWrapper dw = (DataWrapper) itemsList.getSelectedValue();
		return dw.index;
	}

	public Object getSelectedValue() {
		DataWrapper dw = (DataWrapper) itemsList.getSelectedValue();
		return dw.data;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		northJP = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		southJP = new javax.swing.JPanel();
		jButton1 = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		itemsList = new org.jdesktop.swingx.JXList();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		northJP.setPreferredSize(new java.awt.Dimension(400, 30));

		jLabel1.setText("jLabel1");

		javax.swing.GroupLayout northJPLayout = new javax.swing.GroupLayout(northJP);
		northJP.setLayout(northJPLayout);
		northJPLayout.setHorizontalGroup(northJPLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				northJPLayout.createSequentialGroup().addGap(25, 25, 25).addComponent(jLabel1)
						.addContainerGap(330, Short.MAX_VALUE)));
		northJPLayout.setVerticalGroup(northJPLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				northJPLayout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		getContentPane().add(northJP, java.awt.BorderLayout.PAGE_START);

		southJP.setPreferredSize(new java.awt.Dimension(400, 30));

		jButton1.setText("jButton1");

		javax.swing.GroupLayout southJPLayout = new javax.swing.GroupLayout(southJP);
		southJP.setLayout(southJPLayout);
		southJPLayout.setHorizontalGroup(southJPLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)
				.addGroup(
						southJPLayout
								.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										southJPLayout.createSequentialGroup().addGap(0, 156,
												Short.MAX_VALUE).addComponent(jButton1).addGap(0,
												156, Short.MAX_VALUE))));
		southJPLayout.setVerticalGroup(southJPLayout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 30, Short.MAX_VALUE).addGroup(
				southJPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								southJPLayout.createSequentialGroup().addGap(0, 2, Short.MAX_VALUE)
										.addComponent(jButton1).addGap(0, 3, Short.MAX_VALUE))));

		getContentPane().add(southJP, java.awt.BorderLayout.SOUTH);

		itemsList.setPreferredSize(new java.awt.Dimension(43, 400));
		jScrollPane1.setViewportView(itemsList);

		getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				SelectionDlg dialog = new SelectionDlg(new javax.swing.JFrame(), true);

				List<String> data = new ArrayList<String>();
				data.add("aa 1");
				data.add("aa 2");
				data.add("aa 3");

				dialog.setData(data);

				dialog.addWindowListener(new java.awt.event.WindowAdapter() {

					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JList itemsList;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JPanel northJP;
	private javax.swing.JPanel southJP;

	// End of variables declaration//GEN-END:variables

	private static class DataWrapper {
		Object data;
		int index;

		public DataWrapper(Object data, int index) {
			this.data = data;
			this.index = index;
		}

		@Override
		public String toString() {
			return data.toString();
		}

	}
}
